package com.yiman.project.system.service.impl;

import com.yiman.project.system.mapper.RoleMapper;
import com.yiman.project.system.mapper.RoleMenuMapper;
import com.yiman.project.system.model.Role;
import com.yiman.project.system.model.RoleMenuKey;
import com.yiman.project.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Mr.yang
 * @create: 2019-07-10
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleMenuMapper roleMenuMapper;

    @Override
    public List<Role> selectRoleList(Role role) {
        return roleMapper.selectRoleList(role);
    }

    @Override
    public int updateRole(Role role) {

        //ShiroUtils.clearCachedAuthorizationInfo();
        // 删除角色与菜单关联
        if(role.getMenuIds() != null){
            roleMapper.updateByPrimaryKeySelective(role);
            roleMenuMapper.deleteRoleMenuByRoleId(role.getRoleId());
            return insertRoleMenu(role);
        }
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public List<Role> getAllRole() {
        return roleMapper.getAllRole();
    }

    @Override
    public List<Role> selectRolesByUserId(Integer userId) {
        List<Role> userRoles = roleMapper.selectRolesByUserId(userId);
        List<Role> roles = roleMapper.getAllRole();
        for (Role role : roles) {
            for (Role userRole : userRoles) {
                if (role.getRoleId().longValue() == userRole.getRoleId().longValue()) {
                    role.setFlag(true);
                    break;
                }
            }
        }
        return roles;
    }

    @Override
    @Transactional
    public int insertRole(Role role) {
        //role.setCreateBy(ShiroUtils.getLoginName());
        // 新增角色信息
        if(role.getMenuIds() != null){
            roleMapper.insertSelective(role);
            //ShiroUtils.clearCachedAuthorizationInfo();
            return insertRoleMenu(role);
        }
       return  roleMapper.insertSelective(role);
    }

    @Override
    public Role selectByPrimaryKey(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    @Transactional
    public void deleteRoleByIds(String ids) {
        String[] arr = ids.split(",");

        final Integer[] roleIds = new Integer[arr.length];
        for (int i = 0; i < arr.length; i++) {
            final Integer v = Integer.valueOf(arr[i]);
            roleIds[i] = v;
        }

        roleMapper.deleteRoleByIds(roleIds);
        roleMenuMapper.deleteRoleMenuByIds(roleIds);
    }

    /**
     * 新增角色菜单信息
     *
     * @param role 角色对象
     */
    public int insertRoleMenu(Role role)
    {
        int rows = 0;
        // 新增用户与角色管理
        List<RoleMenuKey> list = new ArrayList<>();
        for (Integer menuId : role.getMenuIds()) {
            if(role.getRoleId() != null&&menuId != null){
                RoleMenuKey rm = new RoleMenuKey();
                rm.setRoleId(role.getRoleId());
                rm.setMenuId(menuId);
                list.add(rm);
            }
        }
        if (list.size() > 0) {
            rows = roleMenuMapper.batchRoleMenu(list);
        }
        return rows;
    }
}
